<script setup lang="ts">
import { onMounted, ref } from "vue";
import Searchbox from "@/views/components/searchbox/Searchbox.vue";
import BaseComponent from "@/views/configs/deptConfig/component/base.vue";
import { useLogic } from "@/views/system/user/logic/logic";
import { useRoute } from "vue-router";

defineOptions({
  name: "dept-config",
});

const selected = ref(0);

const {deptTreeList,getDeptTree}  = useLogic()
onMounted(async () => {
  await getDeptTree()
})

function tabClick({ index }) {
  selected.value = index;
}

const listComponents = [
  {
    key: "BaseComponent",
    title: "基础配置",
    component: BaseComponent
  },
];


const searchFields = ref(  [
  {
    prop: 'deptId',
    name: '部门',
    type: 'cascader',
    cascaderOptions: deptTreeList,
    ops: {
      valueField: 'id',
      labelField: 'name',
      emitPath: false,
      checkStrictly: true
    }
  },
])
const searchForm = ref(  {
  deptId: 0,
})

const handleSearch = (e:any) => {
  console.log(e)
  if(e.deptId){
    searchForm.value.deptId = e.deptId;
  }else{
    searchForm.value.deptId = 0
  }
}

const handleReset = (e:any) => {
  searchForm.value.deptId = 0
}

const route = useRoute();
const deptId = ref(route.query.deptId ? Number(route.query.deptId) : 0);
if(deptId.value>0){
  searchForm.value.deptId = deptId.value;
}

</script>

<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <Searchbox
          :fields= searchFields
          :maxVisibleFields=3
          @search="handleSearch"
          @reset="handleReset"
          @change="handleSearch"
        />
      </div>
    </template>

    <el-tabs @tab-click="tabClick" >
      <template v-for="(item, index) of listComponents" :key="item.key">
        <el-tab-pane :lazy="true">
          <template #label>
            <span
              v-tippy="{
                maxWidth: 'none',
                content: `${item.title}`
              }"
            >
              {{ item.title }}
            </span>
          </template>
          <keep-alive>
            <component :is="item.component"
                       :key="item.key"
                       v-show="selected == index"
                       :deptId="searchForm.deptId"/>
          </keep-alive>
        </el-tab-pane>
      </template>
    </el-tabs>

  </el-card>
</template>

<style scoped lang="scss">

</style>




